<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <video class="video" muted id="video" autoplay="autoplay" loop="loop" src="~@/assets/login/bg.mp4"/>
      <div class="side-header">
        <div class="side-header-title" v-html="SYSTEM_TITLE">

        </div>
      </div>
      <div class="site-content">
        <div class="brand-info">
          <h2 class="brand-info__text">{{SYSTEM_TITLE}}</h2>
          <p class="brand-info__intro">{{SYSTEM_MS}}</p>
        </div>
        <div class="login-main">
          <el-form class="login-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" status-icon>
            <el-form-item prop="userName">
              <el-input id="account" v-model="dataForm.userName" :placeholder="$t('账号')"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input id="password" v-model="dataForm.password" type="password" :placeholder="$t('密码')"></el-input>
            </el-form-item>
            <el-form-item prop="captcha">
              <div class="captcha-wrap">
                <el-input id="code" v-model="dataForm.captcha" :placeholder="$t('验证码')">
                </el-input>
                <img class="captcha-img" :src="captchaPath" @click="getCaptcha()" alt="">
              </div>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn-submit" type="primary" @click="dataFormSubmit()">{{$t('登录')}}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <div class="copyright">
      <div>{{$i18n.locale === 'en' ? 'Copyright © Beijing Gold Cascade Landscape Art Co.,Ltd.' : copyRight}}</div>
      <div><a href="https://beian.miit.gov.cn/" target="_blank">
        {{$i18n.locale === 'en' ? 'ICP Beijing Record 14046119' : backCase}}
      </a></div>
    </div>
  </div>
</template>

<script>
import { getUUID } from '@/utils'
import {API_HOST, SYSTEM_TITLE, SYSTEM_MS, LOGIN_TO} from '@/constant'
import {postSysLogin} from '$my/code/api/sys-login'
import {saveTokenToCache} from '@/helper/utils'

export default {
  components: {
  },
  data () {
    return {
      SYSTEM_TITLE,
      SYSTEM_MS,
      dataForm: {
        userName: '',
        password: '',
        uuid: '',
        captcha: ''
      },
      dataRule: {},
      ruleCn: {
        userName: [
          { required: true, message: '帐号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      ruleEn: {
        userName: [
          { required: true, message: 'The account cannot be empty', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'The password cannot be empty', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: 'The verification code cannot be empty', trigger: 'blur' }
        ]
      },
      captchaPath: '',
      copyRight: '',
      backCase: ''
    }
  },
  created () {
    this.getCaptcha()
  },
  methods: {
    // 提交表单
    dataFormSubmit () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          const data = {
            username: this.dataForm.userName,
            password: this.dataForm.password,
            uuid: this.dataForm.uuid,
            captcha: this.dataForm.captcha
          }

          postSysLogin(data).then(res => {
            saveTokenToCache(res.token)
            this.$router.replace(LOGIN_TO)
          }).catch(e => {
            this.getCaptcha()
          })
        }
      })
    },
    // 获取验证码
    getCaptcha () {
      this.dataForm.uuid = getUUID()
      this.captchaPath = `${API_HOST}/captcha.jpg?uuid=${this.dataForm.uuid}`
    },
    //
    setRule() {
      const lang = this.$i18n.locale
      this.dataRule = lang === 'en' ? this.ruleEn : this.ruleCn
    }
  },
  watch: {
    '$i18n.locale'(nval) {
      this.setRule()
    }
  },
  mounted() {
    this.setRule()

    const video = document.querySelector('#video')
    video.play();

  }
}
</script>

<style lang="scss">
.site-wrapper.site-page--login {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  &:before {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    content: "";
    background-image: url(~@/assets/login/bg.jpg);
    background-size: 100% 100%;
  }
  .site-content__wrapper {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
  }
  .video {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    margin: auto;
    right: -100%;
    bottom: -100%;
    top: -100%;
    left: -100%;
    object-fit: cover;
  }
  .side-header {
    position: absolute;
    left: 0;
    top: 0;
    height: 110px;
    width: 100%;
    background: url("~@/assets/login/header.svg") no-repeat;
    background-size: 100% 100%;

    &-title {
      font-size: 40px;
      left: 50%;
      top: 15%;
      position: absolute;
      transform: translateX(-50%);
      letter-spacing: 3px;
      line-height: 1;
    }
  }
  .site-content {
  }
  .brand-info {
    margin: 50px 100px 0 90px;
    color: #fff;
    display: none;
  }
  .brand-info__text {
    margin:  0 0 22px 0;
    font-size: 48px;
    font-weight: 400;
    text-transform : uppercase;
  }
  .brand-info__intro {
    margin: 10px 0;
    font-size: 16px;
    line-height: 1.58;
    opacity: .6;
  }
  .login-main {
    position: absolute;
    z-index: 9;
    padding: 150px 60px 180px;
    width: 670px;
    height: 616px;
    background: url(~@/assets/login/box.png) no-repeat;
    background-size: 100% 100%;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
  }
  .login-title {
    font-size: 16px;
  }
  .login-captcha {
    overflow: hidden;
    > img {
      width: 100%;
      cursor: pointer;
    }
  }
  .login-btn-submit {
    width: 100%;
    margin-top: 30px;
    padding: 0;
    font-size: 22px;
    height: 55px;
    line-height: 55px;
    background-color: #9A0202;
    border: 0;

    &:hover {
      background-color: #c11c1c;
    }
  }
}
.site-wrapper {
  color: #FFF;

  .login-form {
    width: 462px;
    margin: 0 auto;
  }

   .el-input__inner {
      height: 65px;
      line-height: 65px;
      color: #FFF;
      outline: none;
      border: 0;
      padding-left: 80px;
      font-size: 20px;
    }
    .el-input__icon {
      font-size: 22px;
    }
    input:-internal-autofill-selected {
      background-color: transparent !important;
    }

  #account {
    background: url(~@/assets/login/account.png) no-repeat center 50%;
    background-size: 100% 100%;

    &:hover, &:focus {
      background: url(~@/assets/login/account-h.png) no-repeat center 50%;
    }
  }

  #password {
    background: url(~@/assets/login/password.png) no-repeat center 50%;
    background-size: 100% 100%;

    &:hover, &:focus {
      background: url(~@/assets/login/password-h.png) no-repeat center 50%;
    }
  }

  #code {
    background: url(~@/assets/login/code.png) no-repeat center 50%;
    background-size: 100% 100%;
    padding-left: 20px;
    padding-right: 240px;

    &:hover, &:focus {
      background: url(~@/assets/login/code-h.png) no-repeat center 50%;
    }
  }

  .captcha-wrap {
    position: relative;
    .captcha-img {
      position: absolute;
      width: 200px;
      height: 50px;
      top: 7px;
      right: 10px;
    }
  }

}

.copyright {
  position: absolute;
  bottom: 10px;
  font-size: 12px;
  text-align: center;
  width: 100%;
}

.is-en {
  .site-wrapper.site-page--login .login-main {
    background: url(~@/assets/login/box-en.png) no-repeat;
    background-size: 100% 100%;
  }
}
</style>
